import type { ResponseData, ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';
import { GateChannelTrafficData, OptionsItem } from './types';
import { message } from 'antd';

//创建一条通行记录
export async function sendTrafficPassData(
  data?: GateChannelTrafficData,
): Promise<ResponseData<any>> {
  if (data?.type === '1') {
    return await request('/test/gate/traffic/receive/pass_in_channel', {
      method: 'POST',
      data: { ...data, direction: 1 },
    });
  }

  if (data?.type === '2') {
    return await request('/test/gate/traffic/receive/pass_out_channel', {
      method: 'POST',
      data: { ...data, direction: 2 },
    });
  }

  if (data?.type === '3') {
    return await request('/test/gate/traffic/receive/check_pass_in', {
      method: 'POST',
      data: { ...data, direction: 1 },
    });
  }

  if (data?.type === '4') {
    return await request('/test/gate/traffic/receive/check_pass_out', {
      method: 'POST',
      data: { ...data, direction: 2 },
    });
  }

  return Promise.reject({ success: false });
}

/**
 * 获取停车区域列表
 * @param params
 */
export async function getZoneSelectionList(
  params: Pagination & { name?: string; no?: string; tenant_id?: string; address_code?: string },
): Promise<RequestData<OptionsItem>> {
  const res: ResponsePaginationData<{
    id: string;
    name?: string;
  }> = await request('/zone/get_zone_options', {
    params: {
      ...params,

      page: params.page || 1,
      pageSize: params.pageSize || 10,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }

  return {
    success: true,
    total: res.data.total,
    data: res.data.list.map((item) => {
      return { ...item, name: item.name, id: item.id, code: item.id };
    }),
  };
}
